/* StorageManager.java
 * 
 * Alfred Olschnoegger
 * 22.06.2008
 */

package technical.storage;

import java.util.Vector;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

import domain.Hunter;
import exceptions.RBDataException;
import exceptions.RabbitBusterException;

/**
 * TODO description
 * 
 * @date 22.06.2008
 * @author Alfred Olschnoegger
 */

public class StorageManager {
	private static String s_HunterPrefix = "h_";
	private static String s_RBSettings = "RB_Settings";

	public Hunter getHunter(int gameID) throws RabbitBusterException {
		Hunter hunter = new Hunter();
		try {
			Vector data = read(s_HunterPrefix + gameID);
			hunter.setName((String) data.elementAt(0));
			hunter.setPassword(Long.parseLong((String) data.elementAt(1)));
		} catch (Exception e) {
			throw new RBDataException();
		}
		return hunter;
	}

	public boolean isAvailable(int gameID) throws RabbitBusterException {
		return isAvailable(s_HunterPrefix + gameID);
	}

	public boolean isSettingsAvailable() throws RabbitBusterException {
		return isAvailable(s_RBSettings);
	}

	public void writeHunter(int gameID, Hunter hunter) throws RabbitBusterException {
		try {
			Vector data = new Vector();
			data.addElement(hunter.getName());
			data.addElement(Long.toString(hunter.getPassword()));
			write(s_HunterPrefix + gameID, data);
		} catch (Exception e) {
			throw new RBDataException();
		}
	}

	private boolean isAvailable(String recordStore) throws RabbitBusterException {
		String[] recordStores = RecordStore.listRecordStores();
		if (recordStores != null) {
			for (int i = 0; i < recordStores.length; i++) {
				if (recordStores[i].equals(recordStore))
					return true;
			}
		}
		return false;
	}

	private Vector read(String recordStore) throws RecordStoreFullException, RecordStoreNotFoundException, RecordStoreException {
		RecordStore rs = null;
		Vector result = new Vector();
		try {
			rs = RecordStore.openRecordStore(recordStore, false);
			byte data[];
			int len;

			for (int i = 1; i <= rs.getNumRecords(); i++) {
				data = new byte[rs.getRecordSize(i)];
				len = rs.getRecord(i, data, 0);
				result.addElement(new String(data, 0, len));
			}
		} finally {
			rs.closeRecordStore();
		}
		return result;
	}

	private void write(String recordStore, Vector data) throws RecordStoreException {
		// deleting old record store if already existing
		try {
			RecordStore.deleteRecordStore(recordStore);
		} catch (RecordStoreNotFoundException e) {
		}

		RecordStore rs = null;
		try {

			rs = RecordStore.openRecordStore(recordStore, true);
			byte byteData[];
			for (int i = 0; i < data.size(); i++) {
				byteData = ((String) data.elementAt(i)).getBytes();
				rs.addRecord(byteData, 0, byteData.length);
			}
		} finally {
			rs.closeRecordStore();
		}
	}

	/**
	 * reads int setting values.
	 * 
	 * @return
	 */
	public Vector getSettings() throws RabbitBusterException {
		try {
			return read(s_RBSettings);
		} catch (Exception e) {
			throw new RBDataException();
		}
	}

	/**
	 * reads int setting values.
	 * 
	 * @return
	 */
	public void writeSettings(Vector data) throws RabbitBusterException {
		try {
			write(s_RBSettings, data);
		} catch (Exception e) {
			throw new RBDataException();
		}
	}

}

// end of StorageManager.java
